<?php
//游客信息和认证类
class Controller_WWW_Tourist extends Controller_WWW_Template
{
    public $file_path = "upload/icon/";
    public $path = "upload/tourist_pic/";

    public function __construct ($req, $res)
    {
        $this->template = "smarty:www/tourist/index";
        parent::__construct($req, $res);
    }

    public function before ()
    {
        parent::before();
        if ($this->_uid == 0)
        {
            if($this->request->action() == 'confirm'){
                return ;
            }
            $this->request->redirect("/");
        }

        $user = $this->_user->getUserInfo();
        $type = Arr::get($user,'userType');
        if($type != 'TOURIST'){
            $this->request->redirect("/");
        }
    }

    public function action_index ()
    {
        $this->action_info();
    }

    /**
     * 基本信息显示
     */
    public function action_info(){
        $uid = $this->_uid;
        $user_info = User_UserInfo::user_info($uid)->current();
        if(empty($user_info)){
           //$this->request->redirect("/tourist/add_user_info/$uid");
            $this->request->redirect("/tourist/update_user_info/$uid");
        }
        $this->template->set("user",$user_info);
        $this->template->set("step","user_info");

    }

    // 预订记录之活动/路线订单列表
    public function action_orders ( $page = 1)
    {
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        $order  = new Order($this->_user);
        list($count, $orders) = $order->listOrdersByTourist($offset, $limit);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set('countsOrders', $count);
        $this->template->set('orders', $orders);
        $this->template->set('step', $this->request->action().'_track');
    }

    // 订单管理之订单详情(导游的订单)
    public function action_order_detail ($orderNo)
    {
        $orderDetail = Order::detail($orderNo);

        if(empty($orderDetail) || $orderDetail['uid'] != $this->_uid )
        {
            $this->request->redirect('/');
            return;
        }
//        $orderDetail['startDay'] = date('Y.m.d',strtotime($orderDetail['startDay']));
//        $orderDetail['stopDay'] = date('Y.m.d',strtotime($orderDetail['stopDay']));
        $this->template->set('title', '订单详细');
        $this->template->set('order', $orderDetail);
        $this->template->set('step', $this->request->action());
        // 订单日志
        $log  = new Order_Log(Arr::get($orderDetail, 'id'));
        $logs = $log->getLogs();
        $this->template->set('logs', $logs);
    }

    // 添加用户信息
    public function action_add_user_info($uid){
        $user_info = User_UserInfo::user_info($this->_uid)->current();
        if(!empty($user_info)){
            $this->request->redirect("/tourist/info");
        }
        $post = $this->request->post();
        $post['uid'] = $this->_uid;
        if(!empty($post) && !empty($_FILES)){
            list($mag,$status) = User_UserInfo::add_resource($_FILES['userIcon'],$this->file_path);
            if($status){
                $post['userIcon'] = $mag;
                $data['userIcon'] = $mag;
                $data['userGender'] = $post['userGender'];
                $data['userName'] = $post['userFamily'].$post['userName'];
                User_Account::update($data,'uid',$uid);
                if(User_UserInfo::create($post)){
                    $this->request->redirect("/tourist/info");
                }
            }else{
                echo $mag;
                exit;
            }
        }else{
            $this->template->set("uid",$uid);
            $this->template->set("step","add_info");
        }
    }

    public function action_update_user_info($uid){
        $post = $this->request->post();

        if(!empty($post)){
            $data1['userGender'] = trim($post['userGender']);
            $data1['userName'] = trim($post['userFamily']).trim($post['userName']);
            $data1['telNum'] = trim(Arr::get($post, 'userPhone'));
            User_Account::update($data1,'uid',$uid);
            $post['userPhone'] =$post['code'].trim($post['userPhone']);
            unset($post['code']);
            $row = User_UserInfo::update($post,'uid',$uid);
            if($_FILES['userIcon']['name'] != ''){
                list($mag,$status) = User_UserInfo::add_resource($_FILES['userIcon'],$this->file_path);
                if($status){
                    $data['userIcon'] = $mag;
                    User_UserInfo::update($data,'uid',$uid);
                    User_Account::update($data,'uid',$uid);
                }
            }
            $this->request->redirect("/tourist/info");
        }else{
            $this->template->set("uid",$uid);
            $user_info = User_UserInfo::user_info($uid)->current();
            $user_info['telCode']=substr($user_info['userPhone'],0,3);
            $user_info['userPhone']=substr($user_info['userPhone'],3);
            $this->template->set("user",$user_info);
           // $this->template->set("step","edit_info");
             $this->template->set("step","add_info");
           }
    }

    //显示认证界面
    public function action_credit(){
        $uid = $this->_uid;
        $user_info = User_Account::user_account_info($uid)->current();
        $this->template->set("user",$user_info);
        $this->template->set("step","credit");
    }

    //验证用户信息，给用户发邮件
    public function action_subscribe(){
        $this->template->set("step","subscribe");
        $post = $this->request->post();
        if(!empty($post)){
            $data = User_Subscribe::user_activation($post['uid'],$post['email']);
//            var_dump($data);
            $this->template->set("data",$data);
            if(!$data['status']){
                echo "发送失败";
                exit;
            }
        }
    }

    //用户修改认证邮箱
    public function action_subscribe_update($uid){
        $post = $this->request->post();
        if(!empty($post)){
            User_Subscribe::user_activation($post['uid'],$post['email']);
        }else{
            $this->template->set("uid",$uid);
            $this->template->set("step","subscribe_update");
        }

    }
    //验证邮箱是否存在
    public function action_valid_mail(){
    	$post = $this->request->post();
    	$email= Arr::get($post, 'email');
    	$count = DB::select()
    	->from('user_v2')
    	->where('userId','=',$email)
    	->execute()
    	->count();
    	$ret=array('status'=>0,'msg'=>null);
    	if($count==0){
    		$ret['msg']='邮箱可以使用';
    	}else{
    		$ret['msg']='邮箱已经存在';
    		$ret['status']=1;
    	}  		
    	echo json_encode($ret);
    	exit;	
    }
    //验证用户邮件
    public function action_confirm(){
        if($this->_uid > 0) {
            $query = $this->request->query();
            $token = Arr::get($query, 'token');
            list($status, $msg) = User_Subscribe::confirm_token($token);
            $this->template->set("data", $msg);
            $this->template->set("step", "confirm");
            if ($status) {
                $this->template->set("confirm", $msg);
            } else {

            }
        }else{
            $this->template->set_filename('smarty:www/guider/valid_fail');
        }
    }

    public function action_message($page = 1){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;
        list($count,$list) = User_Chat::getTouristChatByUid($uid,$limit,$offset);
//        var_dump($list);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set("pagination",$pagination);
        $this->template->set("list",$list);


        $this->template->set("step",'message_list');
    }

    public function action_message_remove($id){
        $data = User_Chat::removeTouristChatByGuiderId($id);
        if($data > 0){
            $this->request->redirect("tourist/message");
        }
    }



    public function action_notice($page = 1){
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;
        list($counts,$notice) = Util_Notice::noticeList($limit,$offset);
        $pagination = $this->pagination($counts, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set("step","notice");
        $this->template->set("data",$notice);
    }

    public function action_show_notice($nid){
        $ret['status'] = 1;
        if(Util_Notice::noticeDetail($nid)){
            $ret['status'] = 0;
        } ;
        echo json_encode($ret);
        exit;
    }

    public function action_remove_notice($nid){
        $status = Util_Notice::remove($nid);
        if($status){
            $this->request->redirect('/tourist/notice');
        }
    }

    //展示路线评论
    public function action_line_comments($page = 1){
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        list($count,$list) = User_TouristComments::get_track($this->_user,$offset,$limit);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);

        $this->template->set("count",$count);
        $this->template->set("list",$list);
        $this->template->set("step","line_comments");
    }

    //展示活动评论
    public function action_activity_comments($page = 1){
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        list($count,$list) = User_TouristComments::get_track($this->_user,$offset,$limit,'ACTIVITY');
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);

        $this->template->set("count",$count);
        $this->template->set("list",$list);
        $this->template->set("step","activity_comments");
    }

    //添加路线评论
    public function action_add_line_comment($type,$oid){
        $post = $this->request->post();
        if(empty($post)){
            $data = User_TouristComments::tourist_track_check_out('oid',$oid)->current();
            if(!empty($data)){
                $this->request->redirect('/tourist/show_line_comment/'.$data['id']);
            }
            $orderDetail = Order::detail($oid);
            $this->template->set("step","add_line_comment");
            $this->template->set("data",$orderDetail);
        }else{
            list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            if($flag){
                $data['tourist_pic'] = $file;
                $data['uid'] = Arr::get($post,'uid');
                $data['track_id'] = Arr::get($post,'track_id');
                $data['track_name'] = Arr::get($post,'track_name');
                $data['guider_id'] = Arr::get($post,'guider_id');
                $data['track_score'] = Arr::get($post,'track_score');
                $data['persons'] = Arr::get($post,'persons');

                $data['guider_score'] = Arr::get($post,'guider_score');
                $data['comments'] = htmlspecialchars($post['comments']) ;
                $data['created'] = time() ;
                $data['oid'] = Arr::get($post,'id') ;
                $type=trim($type);
                $type=strtoupper($type);
                $data['type'] = $type ;

                User_TouristComments::tourist_track_add($data);
                $where = array(
                    'key'=>'type',
                    'cond'=>'=',
                    'val'=>$type
                );
                //更改订单状态为已评价
                User_TouristComments::updateOrderStatus($data['oid']);
                
                $avg = User_TouristComments::tourist_track_score('track_id',$data['track_id'],$where);
                $save['score'] = $avg*100;
                $log = new Order_Log($oid);
                $log->add("游客进行了评价");
                switch ($type){
                    case 'TRACK':
                        User_TouristComments::update_score($save,'mod_track','id',$data['track_id']);
                	break;
                    case 'ACTIVITY':
                        User_TouristComments::update_score($save,'mod_activity','id',$data['track_id']);
                	break;
                    default:
                    	User_TouristComments::update_score($save,'mod_ziying','id',$data['track_id']);
                    	break;
                }
                
                $this->request->redirect("/tourist/line_comments");
            }
        }
    }

    //添加活动评论
    public function action_add_activity_comment($oid){
        $post = $this->request->post();
        if(empty($post)){
            $data = User_TouristComments::tourist_track_check_out('oid',$oid)->current();
            if(!empty($data)){
                $this->request->redirect('/tourist/show_activity_comment/'.$data['id']);
            }
            $orderDetail = Order::detail($oid);
            $this->template->set("step","add_activity_comment");
            $this->template->set("data",$orderDetail);
        }else{
            list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            if($flag){
                $data['tourist_pic'] = $file;
                $data['uid'] = Arr::get($post,'uid');
                $data['track_id'] = Arr::get($post,'track_id');
                $data['track_name'] = Arr::get($post,'track_name');
                $data['guider_id'] = Arr::get($post,'guider_id');
                $data['track_score'] = Arr::get($post,'track_score');
                $data['persons'] = Arr::get($post,'persons');

                $data['guider_score'] = Arr::get($post,'guider_score');
                $data['comments'] = htmlspecialchars($post['comments']) ;
                $data['created'] = time() ;
                $data['type'] = 'ACTIVITY' ;
                $data['oid'] = Arr::get($post,'id') ;
                User_TouristComments::tourist_track_add($data);
                $where = array(
                    'key'=>'type',
                    'cond'=>'=',
                    'val'=>'ACTIVITY'
                );
                $avg = User_TouristComments::tourist_track_score('track_id',$data['track_id'],$where);
                $save['score'] = $avg*100;
                $log = new Order_Log($oid);
                $log->add("游客进行了评价");
                User_TouristComments::update_score($save,'mod_activity','id',$data['track_id']);
                $this->request->redirect("/tourist/activity_comments");
            }
        }
    }

    //展示路线评论详细
    public function action_show_line_comment($id){
        $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
        if(!empty($data['tourist_pic'])){
        $tourist_pic = explode(';',trim($data['tourist_pic'],';'));
        $this->template->set('tourist_pic', $tourist_pic);
        }
        $this->template->set('data', $data);
        $this->template->set('step', "show_line_comment");
    }

    //展示活动评论详细
    public function action_show_activity_comment($id){
        $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
        $tourist_pic = explode(';',trim($data['tourist_pic'],';'));
//        var_dump($data);
        $this->template->set('data', $data);
        $this->template->set('tourist_pic', $tourist_pic);
        $this->template->set('step', "show_activity_comment");
    }
}
